/**
 * @Description Ext.translate for ExtJS 2.x and 3.x; Auto translation with Google Translation API (java/php server side)
 * @author  Tomislav Milkovic
 * @license LGPLv3 http://www.opensource.org/licenses/lgpl-3.0.html
 * @version 1.8, 12.04.2010.
 */


/**
 * @updated 16.10.2009
 * @description Language Icon COmbob box showing list of supported languages. Serverf side decide if resource names will be displayed also
 * 				what depends on translation editor. Editor will sent additional cookie value to the server so server side can detect editor  
 * 
 * @updated 12.10.2010 Added translation status events to enable/disable combobox while translaiton in process 
 */
Ext.namespace('Ext.translate');

Ext.translate.LanguageCombo = Ext.extend(Ext.ux.form.IconCombo, {
	
    fieldLabel: 'Select Language',
    store: new Ext.data.SimpleStore({
      fields: ['languageCode', 'languageName', 'countryFlag'],
      data: Ext.translate.languages
    }),
    valueField: 'languageName',
    value: Ext.translate.defaultLanguage,
    displayField: 'languageName',
    iconClsField: 'countryFlag',
    triggerAction: 'all',
    editable: false,
    disabled : true ,
    mode: 'local',

    initComponent : function(){
		Ext.translate.LanguageCombo.superclass.initComponent.call(this);
		this.on('select',this.selectEvent,this);
		Ext.translate.LanguageCombo.superclass.initComponent.apply(this, arguments);
	},
	
	onRender : function(){
		Ext.translate.LanguageCombo.superclass.onRender.apply(this, arguments);
		Ext.translate.Manager.getSupportedLanguages(this.renderEvent.createDelegate(this));
	},
	
	renderEvent : function (){
	  this.store.loadData(Ext.translate.languages);
	  Ext.translate.Manager.generateFlagsCSS(Ext.translate.languages);
	  this.enable();	
	  this.setValue(Ext.translate.currentLanguage);
	},
	
	//if event from translate all
	doEnable : function(allComps){
		if (!allComps) return;
		this.enable();
	},
	
	//16.10.2009. - added support for resource strings, if selected resource string, don't apply translations ;just load from server cache
	selectEvent : function (){
		 if (Ext.translate.Manager.currentLanguage == this.getValue() ) return;
		 
		 if(Ext.translate.clientonly){
			 Ext.translate.Cache.on('translationNOTretrieved',Ext.translate.Manager.applyTranslation,this);
			 Ext.translate.Manager.setlanguage(this.getValue());
			return; 
		 };
		 
		 this.disable();
		 
	      var onErr = function(owner,response){

	    	  if (!Ext.translate.Manager.isLanguageChangedToDef()){
	    		//if contains msg then error occured, else ther eis no translation data, but language is supported
	    		  if(Ext.isString(response.msg)) {
	    			  Ext.Msg.alert("Info","Translations not supported for language " + this.getValue() + "<br><br> Error : " + response.msg );
	    		  	  this.setValue(Ext.translate.oldLanguage);
	    		  	  return;
	    		  }
	    		  
	    	  };
	    	  Ext.translate.Manager.applyTranslation();

	      };
	      

	      var callbackFn = function(options, success, response){
	    	  var lng = this.getValue();	      
	    	  var result = Ext.decode(response.responseText);
	    	  if(!success){
	    		  onErr(lng,result).createDelegate(this);
	    		  return;
	    		 }
	    	  	    	  
		      Ext.translate.Manager.setlanguage(lng);
		      
		      if(!result.success & !Ext.translate.translating)
		    	  Ext.translate.Cache.fireEvent('aftertranslate', Ext.translate.Cache,false);
		    	  //Ext.translate.Manager.applyTranslation();
		      
	      };
	      
	      Ext.translate.Cache.on('aftertranslate',this.doEnable,this);
	      Ext.translate.Cache.on('translationNOTretrieved',onErr,this);
	      
	       
          Ext.Ajax.request( {
              url : Ext.translate.URL,
              debug : Ext.translate.debug,
              params : {'action' : Ext.translate.action[0],	'cmd' : 'isNormalService', 'language' : this.getValue() },
              method : 'GET',
              callback : callbackFn,
              scope : this
      });
          
	}
	
});



  
